﻿namespace Sitecore.ChangesTracker.Commands
{
  using System;
  using Sitecore;
  using Applications.WebEdit.Dialogs.DateSelector;
  using Diagnostics;
  using Shell.Framework.Commands;
  using Sitecore.Web;
  using Web.UI.Sheer;
  using Web.UI.WebControls;
  using Web.UI.XamlSharp.Continuations;

  public class SelectEndDate : Command, ISupportsContinuation
  {
    public override void Execute(CommandContext context)
    {
      Assert.ArgumentNotNull(context, "context");
      ContinuationManager.Current.Start(this, "Run");
    }

    protected void Run(ClientPipelineArgs args)
    {
      Assert.ArgumentNotNull(args, "args");
      if (args.IsPostBack)
      {
        if (args.HasResult)
        {
          //WebEditUtil.SetCurrentDate(DateUtil.IsoDateToDateTime(args.Result));

          DateTime current = DateUtil.IsoDateToDateTime(args.Result);
          DateTime start = WebUtil.GetSessionDateTime("cht_sdate", DateTime.Now.Date);
          if (current < start)
          {
            return;
          }
          WebUtil.SetSessionValue("cht_edate", current);
          SheerResponse.SetLocation(string.Empty);
        }
      }
      else
      {
        SelectDateOptions options = new SelectDateOptions();
        options.Header = "Select the archive date";
        options.Text = "Select the date that you want view.";
        options.SelectedDate = WebUtil.GetSessionDateTime("cht_edate", DateTime.Now.Date);
        SheerResponse.ShowModalDialog(options.ToString(), true);
        args.WaitForPostBack();
      }
    }
  }
}
